package jp.heartdokyun.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;

import jp.heartdokyun.model.Level;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmlpull.v1.XmlSerializer;

import android.content.Context;
import android.media.AudioManager;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;

public class Utils {
	public static void saveDataFile(Level playResult) {
		File file = new File(Environment.getExternalStorageDirectory()
				.toString() + "/NaruseKokomi");
		if (!file.exists()) {
			file.mkdirs();
		}
		File newxmlfile = new File(Environment.getExternalStorageDirectory()
				.toString() + "/NaruseKokomi/setting.xml");
		try {
			if (!newxmlfile.exists()) {
				newxmlfile.createNewFile();
			}
		} catch (IOException e) {
			Log.e("IOException", "Exception in create new File");
		}

		ArrayList<Level> mArrPlayResult = removeDuplicate(playResult);

		XmlSerializer serializer = Xml.newSerializer();

		try {

			FileOutputStream fileos = null;
			try {
				fileos = new FileOutputStream(newxmlfile, false);
			} catch (FileNotFoundException e) {
				Log.e("FileNotFoundException", e.toString());
			}

			serializer.setOutput(fileos, "UTF-8");
			serializer.startDocument(null, Boolean.valueOf(true));
			serializer.setFeature(
					"http://xmlpull.org/v1/doc/features.html#indent-output",
					true);

			for (int i = 0; i < mArrPlayResult.size(); i++) {
				serializer.startTag(null, mArrPlayResult.get(i).getType());
				serializer.attribute(null, "level", mArrPlayResult.get(i)
						.getLevel());
				serializer.attribute(null, "levelname", mArrPlayResult.get(i)
						.getLevelName());
				serializer.attribute(null, "title", mArrPlayResult.get(i)
						.getTitle());
				serializer.attribute(null, "rank", mArrPlayResult.get(i)
						.getRank());
				serializer.attribute(null, "time", mArrPlayResult.get(i)
						.getTime());
				serializer.endTag(null, mArrPlayResult.get(i).getType());
			}
			serializer.endDocument();
			serializer.flush();
			fileos.close();
		} catch (Exception e) {
			Log.e("Exception", "Exception occured in wroting");
		}
	}

	public static ArrayList<Level> removeDuplicate(Level playResult) {
		ArrayList<Level> mArrPlayResult = getDataObj();
		boolean isDuplicate = false;

		for (int i = 0; i < mArrPlayResult.size(); i++) {
			if ((mArrPlayResult.get(i).getLevel().equals(playResult.getLevel()))
					&& (mArrPlayResult.get(i).getType().equals(playResult
							.getType()))) {
				mArrPlayResult.get(i).setTitle(playResult.getTitle());
				mArrPlayResult.get(i).setRank(playResult.getRank());
				mArrPlayResult.get(i).setTime(playResult.getTime());
				isDuplicate = true;
			}
		}
		if (!isDuplicate) {
			mArrPlayResult.add(playResult);
		}
		return mArrPlayResult;
	}

	public static ArrayList<Level> getDataObj() {
		ArrayList<Level> mArrPlayResult = new ArrayList<Level>();

		try {
			String path;
			path = Environment.getExternalStorageDirectory().toString();
			XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
			factory.setNamespaceAware(true);
			XmlPullParser xpp = factory.newPullParser();
			// get a reference to the file.
			File file = new File(path + "/NaruseKokomi");
			if (!file.exists()) {
				file.mkdirs();
			}
			File newxmlfile = new File(path + "/NaruseKokomi/setting.xml");

			if (!newxmlfile.exists()) {
				newxmlfile.createNewFile();
			}

			// create an input stream to be read by the stream reader.
			FileInputStream fis = new FileInputStream(newxmlfile);
			xpp.setInput(new InputStreamReader(fis));
			int eventType = xpp.getEventType();

			while (eventType != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_TAG) {
					Level tmpResult = new Level(xpp.getName(),
							xpp.getAttributeValue(0), xpp.getAttributeValue(1),
							xpp.getAttributeValue(2), xpp.getAttributeValue(3),
							xpp.getAttributeValue(4));
					// tmpResult.setType(xpp.getName());
					// tmpResult.setLevel(xpp.getAttributeValue(0));
					// tmpResult.setRank(xpp.getAttributeValue(1));
					// tmpResult.setTime(xpp.getAttributeValue(2));
					mArrPlayResult.add(tmpResult);
				}
				eventType = xpp.next();
			}
		} catch (IOException e) {
			Log.e("IOException", "Exception in create new File");
		} catch (Exception e) {
			Log.e("Exception", "Exception occured in wroting");
		}

		return mArrPlayResult;
	}

	public static boolean isExistType(String type) {
		ArrayList<Level> mArrPlayResult = getDataObj();
		boolean isExist = false;
		for (int i = 0; i < mArrPlayResult.size(); i++) {
			if (mArrPlayResult.get(i).getType().equals(type)) {
				isExist = true;
			}
		}

		return isExist;
	}

	public static ArrayList<Level> getListLevelFromType(String type) {
		ArrayList<Level> mArrPlayResult = getDataObj();
		ArrayList<Level> tmpArrPlayResult = new ArrayList<Level>();
		for (int i = 0; i < mArrPlayResult.size(); i++) {
			if (mArrPlayResult.get(i).getType().equals(type)) {
				tmpArrPlayResult.add(mArrPlayResult.get(i));
			}
		}
		return tmpArrPlayResult;
	}

	public static void deleteRecursive(File fileOrDirectory) {
		if (fileOrDirectory.isDirectory())
			for (File child : fileOrDirectory.listFiles())
				deleteRecursive(child);
		fileOrDirectory.delete();

	}

	public static void addLevel(Context ctx, String type) {
		if (type.equals("ol")) {
			setTypeOL(ctx, type);
		} else if (type.equals("nurse")) {
			setTypeNurse(ctx, type);
		} else if (type.equals("ca")) {
			setTypeCA(ctx, type);
		} else if (type.equals("sailor")) {
			setTypeSailor(ctx, type);
		} else if (type.equals("maid")) {
			setTypeMaid(ctx, type);
		} else if (type.equals("teacher")) {
			setTypeTeacher(ctx, type);
		} else if (type.equals("minikimono")) {
			setTypeMinikimono(ctx, type);
		} else if (type.equals("bloomer")) {
			setTypeBloomer(ctx, type);
		}
	}

	private static void setType(String type, String[] arrTypevalue,
			String[] arrTypename) {
		ArrayList<Level> listLevel = Utils.getListLevelFromType(type);
		int j = listLevel.size();
		int k = 0;
		for (int i = 0; i < arrTypevalue.length; i++) {
			Level level = new Level();
			level.setType(type);
			level.setLevel(Integer.toString(i + 1));
			level.setLevelName(arrTypevalue[i]);
			;
			if (j == 0) {
				if (i == 0) {
					level.setTitle(arrTypename[i]);
					level.setRank("");
					level.setTime("");
				} else {
					level.setTitle("");
					level.setRank("");
					level.setTime("");
				}
			} else {
				if (!listLevel.get(i).getTime().equals("")) {
					level.setTitle(arrTypename[i]);
					level.setRank(listLevel.get(i).getRank());
					level.setTime(listLevel.get(i).getTime());
					k++;
				} else {
					if (i == (k)) {
						level.setTitle(arrTypename[i]);
						level.setRank("");
						level.setTime("");
					} else {
						level.setTitle("");
						level.setRank("");
						level.setTime("");
					}
				}
			}
			Utils.saveDataFile(level);
		}
	}

	private static void setTypeOL(Context ctx, String type) {
		ArrayList<Level> listLevel = new ArrayList<Level>();

		String[] arrTypevalue = ctx.getResources().getStringArray(
				ctx.getResources().getIdentifier("type_" + type + "_value",
						"array", "jp.heartdokyun"));
		String[] arrTypename = ctx.getResources().getStringArray(
				ctx.getResources().getIdentifier("type_" + type + "_name",
						"array", "jp.heartdokyun"));
		listLevel = Utils.getListLevelFromType(type);
		setType(type, arrTypevalue, arrTypename);
	}

	private static void setTypeNurse(Context ctx, String type) {
		ArrayList<Level> listLevel = new ArrayList<Level>();

		String[] arrTypevalue = ctx.getResources().getStringArray(
				ctx.getResources().getIdentifier("type_" + type + "_value",
						"array", "jp.heartdokyun"));
		String[] arrTypename = ctx.getResources().getStringArray(
				ctx.getResources().getIdentifier("type_" + type + "_name",
						"array", "jp.heartdokyun"));
		listLevel = Utils.getListLevelFromType(type);
		setType(type, arrTypevalue, arrTypename);
	}

	private static void setTypeSailor(Context ctx, String type) {
		ArrayList<Level> listLevel = new ArrayList<Level>();

		String[] arrTypevalue = ctx.getResources().getStringArray(
				ctx.getResources().getIdentifier("type_" + type + "_value",
						"array", "jp.heartdokyun"));
		String[] arrTypename = ctx.getResources().getStringArray(
				ctx.getResources().getIdentifier("type_" + type + "_name",
						"array", "jp.heartdokyun"));
		listLevel = Utils.getListLevelFromType(type);
		setType(type, arrTypevalue, arrTypename);
	}

	private static void setTypeCA(Context ctx, String type) {
		ArrayList<Level> listLevel = new ArrayList<Level>();

		String[] arrTypevalue = ctx.getResources().getStringArray(
				ctx.getResources().getIdentifier("type_" + type + "_value",
						"array", "jp.heartdokyun"));
		String[] arrTypename = ctx.getResources().getStringArray(
				ctx.getResources().getIdentifier("type_" + type + "_name",
						"array", "jp.heartdokyun"));
		listLevel = Utils.getListLevelFromType(type);
		setType(type, arrTypevalue, arrTypename);
	}

	private static void setTypeMaid(Context ctx, String type) {
		ArrayList<Level> listLevel = new ArrayList<Level>();

		String[] arrTypevalue = ctx.getResources().getStringArray(
				ctx.getResources().getIdentifier("type_" + type + "_value",
						"array", "jp.heartdokyun"));
		String[] arrTypename = ctx.getResources().getStringArray(
				ctx.getResources().getIdentifier("type_" + type + "_name",
						"array", "jp.heartdokyun"));
		listLevel = Utils.getListLevelFromType(type);
		setType(type, arrTypevalue, arrTypename);
	}

	private static void setTypeTeacher(Context ctx, String type) {
		ArrayList<Level> listLevel = new ArrayList<Level>();

		String[] arrTypevalue = ctx.getResources().getStringArray(
				ctx.getResources().getIdentifier("type_" + type + "_value",
						"array", "jp.heartdokyun"));
		String[] arrTypename = ctx.getResources().getStringArray(
				ctx.getResources().getIdentifier("type_" + type + "_name",
						"array", "jp.heartdokyun"));
		listLevel = Utils.getListLevelFromType(type);
		setType(type, arrTypevalue, arrTypename);
	}

	private static void setTypeMinikimono(Context ctx, String type) {
		ArrayList<Level> listLevel = new ArrayList<Level>();

		String[] arrTypevalue = ctx.getResources().getStringArray(
				ctx.getResources().getIdentifier("type_" + type + "_value",
						"array", "jp.heartdokyun"));
		String[] arrTypename = ctx.getResources().getStringArray(
				ctx.getResources().getIdentifier("type_" + type + "_name",
						"array", "jp.heartdokyun"));
		listLevel = Utils.getListLevelFromType(type);
		setType(type, arrTypevalue, arrTypename);
	}

	private static void setTypeBloomer(Context ctx, String type) {
		ArrayList<Level> listLevel = new ArrayList<Level>();

		String[] arrTypevalue = ctx.getResources().getStringArray(
				ctx.getResources().getIdentifier("type_" + type + "_value",
						"array", "jp.heartdokyun"));
		String[] arrTypename = ctx.getResources().getStringArray(
				ctx.getResources().getIdentifier("type_" + type + "_name",
						"array", "jp.heartdokyun"));
		listLevel = Utils.getListLevelFromType(type);
		setType(type, arrTypevalue, arrTypename);
	}

	// private static void setTypeCA(Context ctx, String type) {
	// ArrayList<Level> listLevel = new ArrayList<Level>();
	// String[] arrTypevalue = ctx.getResources().getStringArray(
	// R.array.type_ca_value);
	// String[] arrTypename = ctx.getResources().getStringArray(
	// R.array.type_ca_name);
	// listLevel = Utils.getListLevelFromType(type);
	// int j = listLevel.size();
	// if (j == 0) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(j + 1));
	// level.setLevelName(arrTypevalue[j]);
	// level.setTitle(arrTypename[j]);
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// } else {
	// int k = 0;
	// for (int i = 0; i < arrTypevalue.length; i++) {
	// if (!listLevel.get(i).getTime().equals("")) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle(arrTypename[i]);
	// level.setRank(listLevel.get(i).getRank());
	// level.setTime(listLevel.get(i).getTime());
	// Utils.saveDataFile(level);
	// k++;
	//
	// } else {
	// if (i == (k + 1)) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle(arrTypename[i]);
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// }else{
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle("");
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// }
	// }
	//
	// }
	// }
	// }
	//
	// private static void setTypeNurse(Context ctx, String type) {
	// ArrayList<Level> listLevel = new ArrayList<Level>();
	// String[] arrTypevalue = ctx.getResources().getStringArray(
	// R.array.type_nurse_value);
	// String[] arrTypename = ctx.getResources().getStringArray(
	// R.array.type_nurse_name);
	// listLevel = Utils.getListLevelFromType(type);
	// int j = listLevel.size();
	// if (j == 0) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(j + 1));
	// level.setLevelName(arrTypevalue[j]);
	// level.setTitle(arrTypename[j]);
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// } else {
	// for (int i = 0; i < arrTypevalue.length; i++) {
	// int k = 0;
	// if (!listLevel.get(i).getTime().equals("")) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle(arrTypename[i]);
	// level.setRank(listLevel.get(i).getRank());
	// level.setTime(listLevel.get(i).getTime());
	// Utils.saveDataFile(level);
	// k++;
	//
	// } else {
	// if (i == (k + 1)) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle(arrTypename[i]);
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// }else{
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle("");
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// }
	// }
	//
	// }
	// }
	// }
	//
	// private static void setTypeSailor(Context ctx, String type) {
	// ArrayList<Level> listLevel = new ArrayList<Level>();
	// String[] arrTypevalue = ctx.getResources().getStringArray(
	// R.array.type_sailor_value);
	// String[] arrTypename = ctx.getResources().getStringArray(
	// R.array.type_sailor_name);
	// listLevel = Utils.getListLevelFromType(type);
	// int j = listLevel.size();
	// if (j == 0) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(j + 1));
	// level.setLevelName(arrTypevalue[j]);
	// level.setTitle(arrTypename[j]);
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// } else {
	// for (int i = 0; i < arrTypevalue.length; i++) {
	// int k = 0;
	// if (!listLevel.get(i).getTime().equals("")) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle(arrTypename[i]);
	// level.setRank(listLevel.get(i).getRank());
	// level.setTime(listLevel.get(i).getTime());
	// Utils.saveDataFile(level);
	// k++;
	//
	// } else {
	// if (i == (k + 1)) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle(arrTypename[i]);
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// }else{
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle("");
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// }
	// }
	//
	// }
	// }
	// }
	//
	// private static void setTypeMaid(Context ctx, String type) {
	// ArrayList<Level> listLevel = new ArrayList<Level>();
	// String[] arrTypevalue = ctx.getResources().getStringArray(
	// R.array.type_maid_value);
	// String[] arrTypename = ctx.getResources().getStringArray(
	// R.array.type_maid_name);
	// listLevel = Utils.getListLevelFromType(type);
	// int j = listLevel.size();
	// if (j == 0) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(j + 1));
	// level.setLevelName(arrTypevalue[j]);
	// level.setTitle(arrTypename[j]);
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// } else {
	// for (int i = 0; i < arrTypevalue.length; i++) {
	// int k = 0;
	// if (!listLevel.get(i).getTime().equals("")) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle(arrTypename[i]);
	// level.setRank(listLevel.get(i).getRank());
	// level.setTime(listLevel.get(i).getTime());
	// Utils.saveDataFile(level);
	// k++;
	//
	// } else {
	// if (i == (k + 1)) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle(arrTypename[i]);
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// }else{
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle("");
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// }
	// }
	//
	// }
	// }
	// }
	//
	// private static void setTypeTeacher(Context ctx, String type) {
	// ArrayList<Level> listLevel = new ArrayList<Level>();
	// String[] arrTypevalue = ctx.getResources().getStringArray(
	// R.array.type_teacher_value);
	// String[] arrTypename = ctx.getResources().getStringArray(
	// R.array.type_teacher_name);
	// listLevel = Utils.getListLevelFromType(type);
	// int j = listLevel.size();
	// if (j == 0) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(j + 1));
	// level.setLevelName(arrTypevalue[j]);
	// level.setTitle(arrTypename[j]);
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// } else {
	// for (int i = 0; i < arrTypevalue.length; i++) {
	// int k = 0;
	// if (!listLevel.get(i).getTime().equals("")) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle(arrTypename[i]);
	// level.setRank(listLevel.get(i).getRank());
	// level.setTime(listLevel.get(i).getTime());
	// Utils.saveDataFile(level);
	// k++;
	//
	// } else {
	// if (i == (k + 1)) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle(arrTypename[i]);
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// }else{
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle("");
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// }
	// }
	//
	// }
	// }
	// }
	//
	// private static void setTypeMinikimono(Context ctx, String type) {
	// ArrayList<Level> listLevel = new ArrayList<Level>();
	// String[] arrTypevalue = ctx.getResources().getStringArray(
	// R.array.type_minikimono_value);
	// String[] arrTypename = ctx.getResources().getStringArray(
	// R.array.type_minikimono_name);
	// listLevel = Utils.getListLevelFromType(type);
	// int j = listLevel.size();
	// if (j == 0) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(j + 1));
	// level.setLevelName(arrTypevalue[j]);
	// level.setTitle(arrTypename[j]);
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// } else {
	// for (int i = 0; i < arrTypevalue.length; i++) {
	// int k = 0;
	// if (!listLevel.get(i).getTime().equals("")) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle(arrTypename[i]);
	// level.setRank(listLevel.get(i).getRank());
	// level.setTime(listLevel.get(i).getTime());
	// Utils.saveDataFile(level);
	// k++;
	//
	// } else {
	// if (i == (k + 1)) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle(arrTypename[i]);
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// }else{
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle("");
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// }
	// }
	//
	// }
	// }
	// }
	//
	// private static void setTypeBloomer(Context ctx, String type) {
	// ArrayList<Level> listLevel = new ArrayList<Level>();
	// String[] arrTypevalue = ctx.getResources().getStringArray(
	// R.array.type_bloomer_value);
	// String[] arrTypename = ctx.getResources().getStringArray(
	// R.array.type_bloomer_name);
	// listLevel = Utils.getListLevelFromType(type);
	// int j = listLevel.size();
	// if (j == 0) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(j + 1));
	// level.setLevelName(arrTypevalue[j]);
	// level.setTitle(arrTypename[j]);
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// } else {
	// for (int i = 0; i < arrTypevalue.length; i++) {
	// int k = 0;
	// if (!listLevel.get(i).getTime().equals("")) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle(arrTypename[i]);
	// level.setRank(listLevel.get(i).getRank());
	// level.setTime(listLevel.get(i).getTime());
	// Utils.saveDataFile(level);
	// k++;
	//
	// } else {
	// if (i == (k + 1)) {
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle(arrTypename[i]);
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// }else{
	// Level level = new Level();
	// level.setType(type);
	// level.setLevel(Integer.toString(i + 1));
	// level.setLevelName(arrTypevalue[i]);
	// level.setTitle("");
	// level.setRank("");
	// level.setTime("");
	// Utils.saveDataFile(level);
	// }
	// }
	//
	// }
	// }
	// }
	public static float getBGMVolume(Context ctx) {
		AudioManager mgr = (AudioManager) ctx
				.getSystemService(Context.AUDIO_SERVICE);
		float streamVolumeMax = mgr
				.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		float streamVolumeCurrent = mgr
				.getStreamVolume(AudioManager.STREAM_MUSIC);
		// Add 20% to current volume
		// streamVolumeCurrent += streamVolumeCurrent * 20 / 100;
		if (streamVolumeCurrent >= streamVolumeMax)
			return streamVolumeMax;
		else
			return streamVolumeCurrent;
	}

	public static float getVoiceVolume(Context ctx) {
		AudioManager mgr = (AudioManager) ctx
				.getSystemService(Context.AUDIO_SERVICE);
		float streamVolumeMax = mgr
				.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
		float streamVolumeCurrent = mgr
				.getStreamVolume(AudioManager.STREAM_MUSIC);
		// Add 20% to current volume
		streamVolumeCurrent += streamVolumeCurrent * 20 / 100;
		if (streamVolumeCurrent >= streamVolumeMax)
			return streamVolumeMax;
		else
			return streamVolumeCurrent;
	}
	
	public static void CopyStream(InputStream is, OutputStream os) {
		final int buffer_size = 1024;
		try {
			byte[] bytes = new byte[buffer_size];
			for (;;) {
				int count = is.read(bytes, 0, buffer_size);
				if (count == -1)
					break;
				os.write(bytes, 0, count);
			}
		} catch (Exception ex) {
		}
	}
}
